//index.js
//获取应用实例
const app = getApp();
// 这里写你的js路径
var QQMapWX = require('../../utils/qqmap-wx-jssdk.min.js');
var qqmapsdk;
var touchDot = 0,
  time = 0;
var interval = "";
var tmpFlag = true;
var nthMax = 7,
  nth = 0;

Page({
  data: {
    province: '',
    city: '',
    weatherData: '',
    inputingCity: '',
    // future: []
  },
  onLoad: function () {
    // 这里写你自己的秘钥
    qqmapsdk = new QQMapWX({
      key: 'VJKBZ-VG6C2-VOJUZ-CMIA3-2BICQ-T7B6Y' //自己的key秘钥 http://lbs.qq.com/console/mykey.html 在这个网址申请
    });

    // this.getWeather()
  },
  onShow: function () {
    let vm = this;
    vm.getUserLocation();
  },
  getUserLocation: function () {
    let vm = this;
    wx.getSetting({
      success: (res) => {
        console.log("getSetting");
        console.log(res)
        // res.authSetting['scope.userLocation'] == undefined    表示 初始化进入该页面
        // res.authSetting['scope.userLocation'] == false    表示 非初始化进入该页面,且未授权
        // res.authSetting['scope.userLocation'] == true    表示 地理位置授权
        if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) {
          wx.showModal({
            title: '请求授权当前位置',
            content: '需要获取您的地理位置，请确认授权',
            success: function (res) {
              if (res.cancel) {
                wx.showToast({
                  title: '拒绝授权',
                  icon: 'none',
                  duration: 1000
                })
              } else if (res.confirm) {
                wx.openSetting({
                  success: function (dataAu) {
                    if (dataAu.authSetting["scope.userLocation"] == true) {
                      wx.showToast({
                        title: '授权成功',
                        icon: 'success',
                        duration: 1000
                      })
                      //再次授权，调用wx.getLocation的API
                      vm.getLocation();
                    } else {
                      wx.showToast({
                        title: '授权失败',
                        icon: 'none',
                        duration: 1000
                      })
                    }
                  }
                })
              }
            }
          })
        } else if (res.authSetting['scope.userLocation'] == undefined) {
          //调用wx.getLocation的API
          vm.getLocation();
        } else {
          //调用wx.getLocation的API
          vm.getLocation();
        }
      }
    })
  },
  // 微信获得经纬度
  getLocation: function () {
    let vm = this;
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        console.log("getLocation");
        console.log(res)
        var latitude = res.latitude
        var longitude = res.longitude
        var speed = res.speed
        var accuracy = res.accuracy;
        vm.getLocal(latitude, longitude)
      },
      fail: function (res) {
        console.log('fail' + JSON.stringify(res))
      }
    })
  },
  // 获取当前地理位置
  getLocal: function (latitude, longitude) {
    let vm = this;
    qqmapsdk.reverseGeocoder({
      location: {
        latitude: latitude,
        longitude: longitude
      },
      success: function (res) {
        console.log("getLocal");
        console.log(res);
        let province = res.result.ad_info.province
        let city = res.result.ad_info.city
        vm.setData({
          province: province,
          city: city,
          latitude: latitude,
          longitude: longitude
        }),

          vm.getWeather(city, province)

      },
      fail: function (res) {
        console.log("fail");
        console.log(res);
      },
      complete: function (res) {
        // console.log(res);
      }
    });
  },

  getWeather: function (city, province) {
    var that = this;
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: app.globalData.serverUrl + app.globalData.apiVersion + '/service/weather',
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },

      data: {
        location: city,
        province: province
      },
      success: function (res) {
        var future = res.data.data[1].daily_forecast;
        var weatherData = res.data.data[0];
        console.log(res.data.data)
        console.log(future)
        that.setData({
          weatherData: weatherData,
          future: future
        })
        wx.hideLoading()
      },

      fail:function(res){
          wx.showToast({
            title: '服务器出错',
            icon:'fail'
          })
      }
    })
  },

  inputing: function (e) {
    console.log(e.detail.value)
    this.setData({
      inputingCity: e.detail.value
    })
  },

  bindSearch: function () {
    this.getWeather(this.data.inputingCity)
  },


  // touchStart:function(e){
  //   touchDot = e.touches[0].pageX;

  //   interval = setInterval(function(){
  //     time++;
  //   },100);
  // },

  // touchMove:function(e){
  //   var touchMove = e.touches[0].pageX;
  //   if(touchMove - touchDot <= -40 && time < 10){//向左滑动
  //     if(tmpFlag && nth < nthMax){

  //     }
  //   }
  //   if (touchMove - touchDot >= 40 && time < 10) {//向右滑动
  //     if (tmpFlag && nth > 0) {

  //     }
  //   }   
  // },

  // touchEnd:function(e){
  //   clearInterval(interval);
  //   time = 0;
  //   tmpFlag = true;
  // },


  onPullDownRefresh: function () {
    wx.showLoading({
      title: '加载中',
    })
    this.getUserLocation()
  }
})